package com.xh.bussiness.permission.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.permission.bean.request.UserCommonMenuRequest;
import com.xh.bussiness.permission.dao.UserCommonMenuMapper;
import com.xh.bussiness.permission.model.UserCommonMenu;
import com.xh.bussiness.permission.service.IUserCommonMenuService;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.CacheUtils;
import org.springframework.stereotype.Service;
import com.xh.core.bean.XhPage;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户常用菜单 服务实现类
 * </p>
 *
 * @author Pitt
 * @since 2020-12-03
 */
@Service
public class UserCommonMenuServiceImpl extends BaseServiceImpl<UserCommonMenuMapper, UserCommonMenu> implements IUserCommonMenuService {
    @Override
    public XhPage queryUserCommonMenuPage(XhPage page, UserCommonMenuRequest bean) {
        QueryWrapper qw = new QueryWrapper<UserCommonMenu>();
        //拼装条件
        qw = prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page, qw);
    }

    @Override
    public List queryUserCommonMenu(UserCommonMenuRequest bean) {
        QueryWrapper qw = new QueryWrapper<UserCommonMenu>();
        //拼装条件
        qw = prepareParams(bean, qw);

        return this.dao.selectList(qw);
    }

    @Override
    public boolean addUserCommonMenu(UserCommonMenu userCommonMenu) {
        //先利用缓存，不存库
        List<UserCommonMenu> data = (List<UserCommonMenu>) CacheUtils.get("user_common_menu_" + userCommonMenu.getUserId());
        if (CollectionUtils.isEmpty(data)) {
            data = new ArrayList<>();
            data.add(userCommonMenu);
            CacheUtils.set("user_common_menu_" + userCommonMenu.getUserId(), data);
        } else {
            long count = data.stream().filter(item -> item.getMenuPath().equals(userCommonMenu.getMenuPath())).count();
            if (count > 0) {
                List<UserCommonMenu> filterDatas = data.stream().filter(item -> !item.getMenuPath().equals(userCommonMenu.getMenuPath())).collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(filterDatas)) {
                    filterDatas.add(0, userCommonMenu);
                } else {
                    filterDatas.add(userCommonMenu);
                }
                CacheUtils.set("user_common_menu_" + userCommonMenu.getUserId(), filterDatas);
            } else {
                data.add(0, userCommonMenu);
                CacheUtils.set("user_common_menu_" + userCommonMenu.getUserId(), data);
            }
        }
        return true;
    }

    @Override
    public List<UserCommonMenu> queryUserCommonMenuByUserId(String userId) {
        List<UserCommonMenu> data = (List<UserCommonMenu>) CacheUtils.get("user_common_menu_" + userId);
        if (CollectionUtils.isEmpty(data)) {
            data = new ArrayList<>();
            //设置默认快捷方式
            UserCommonMenu userCommonMenu = new UserCommonMenu();
            userCommonMenu.setUserId(userId);
            userCommonMenu.setMenuTitle("备授课");
            userCommonMenu.setMenuPath("/prelesson/courses");

            UserCommonMenu userCommonMenu1 = new UserCommonMenu();
            userCommonMenu1.setUserId(userId);
            userCommonMenu1.setMenuTitle("我的班级");
            userCommonMenu1.setMenuPath("/");

            UserCommonMenu userCommonMenu2 = new UserCommonMenu();
            userCommonMenu2.setUserId(userId);
            userCommonMenu2.setMenuTitle("题库");
            userCommonMenu2.setMenuPath("/question");

            UserCommonMenu userCommonMenu3 = new UserCommonMenu();
            userCommonMenu3.setUserId(userId);
            userCommonMenu3.setMenuTitle("资料库");
            userCommonMenu3.setMenuPath("/resourceBase");

            UserCommonMenu userCommonMenu4 = new UserCommonMenu();
            userCommonMenu4.setUserId(userId);
            userCommonMenu4.setMenuTitle("试卷库");
            userCommonMenu4.setMenuPath("/paper/testPaper");

            UserCommonMenu userCommonMenu5 = new UserCommonMenu();
            userCommonMenu5.setUserId(userId);
            userCommonMenu5.setMenuTitle("课程管理");
            userCommonMenu5.setMenuPath("");

            data.add(userCommonMenu);
            data.add(userCommonMenu1);
            data.add(userCommonMenu2);
            data.add(userCommonMenu3);
            data.add(userCommonMenu4);
            data.add(userCommonMenu5);
        }
        return data;
    }


    /**
     * 准备条件
     *
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(UserCommonMenuRequest bean, QueryWrapper qw) {
        if (bean.getIds() != null && bean.getIds().length > 0) {
            qw.in("id", bean.getIds());
        }
        return qw;
    }
}
